package com.um.jdy.app.shop.manager;


import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import com.um.jdy.app.shop.entity.dto.GoodsCommentPageDTO;
import com.um.jdy.app.shop.entity.vo.GoodsCommentItemVO;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.shop.code.ShopOrderCode;
import com.um.jdy.common.shop.entity.po.OrderComment;
import com.um.jdy.common.shop.entity.po.OrderCommentApprove;
import com.um.jdy.common.shop.service.OrderCommentApproveService;
import com.um.jdy.common.shop.service.OrderCommentService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.exception.ServiceException;
import com.um.springboot.starter.utils.condition.ConditionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class GoodsCommentManager {

    @Autowired
    MemberUserService memberUserService;

    @Autowired
    OrderCommentService orderCommentService;

    @Autowired
    OrderCommentApproveService orderCommentApproveService;

    /**
     * 分页查询商品评价
     * @param dto
     * @return
     */
    public Page<GoodsCommentItemVO> getGoodsCommentPage(GoodsCommentPageDTO dto){
        Page page = new Page(dto);

        String type = null, isImg = null;
        if(GoodsCommentPageDTO.Type.Media == dto.getType()){
            isImg = YesOrNo.Yes.name();
        }else if(GoodsCommentPageDTO.Type.Text == dto.getType()){
            isImg = YesOrNo.No.name();
        } else if(null != dto.getType()) {
            type = dto.getType().name();
        }

        Map<String, String> condition = ConditionBuilder.create()
                .put("g.shop_goods_id", dto.getShop_goods_id())
                .put("c.type", type)
                .put("c.is_img", isImg)
                .put("c.is_quality", dto.getIs_quality())
                .build();

        List<String> orderNames = ListUtil.toList("c.create_date");
        List<String> sortOns = ListUtil.toList("desc");
        if(null != dto.getApprove_order()){
            orderNames.add(0, "a.approve_num");
            sortOns.add(0, dto.getApprove_order().name());
        }

        return orderCommentService.getCMSViewModelPageBySqlName(page, "get_goods_comment_page", "get_goods_comment_count",
                GoodsCommentItemVO.class, condition.keySet().toArray(new String[0]), condition.values().toArray(new String[0]),
                new ArrayList(), orderNames.toArray(new String[0]), sortOns.toArray(new String[0]), new String[]{memberUserService.get().getMemberUserId(),memberUserService.getComId()}, false);
    }

    /**
     * 点赞
     * @param shopOrderCommentId
     * @return
     */
    public void approve(String shopOrderCommentId) {
        OrderComment comment = orderCommentService.getEntity(shopOrderCommentId);

        if(null == comment){
            throw new ServiceException(ShopOrderCode.CommentNotExist);
        }

        String userId = memberUserService.get().getMemberUserId();
        OrderCommentApprove approve = orderCommentApproveService.getEntity(new String[]{"member_user_id",
                "shop_order_comment_id"}, new Object[]{userId, shopOrderCommentId});
        if(null != approve){
            comment.setApproveNum(comment.getApproveNum() - 1);
            orderCommentApproveService.delete(approve);
        }else{
            comment.setApproveNum(comment.getApproveNum() + 1);
            approve = new OrderCommentApprove();
            approve.setMemberUserId(userId);
            approve.setShopOrderCommentId(shopOrderCommentId);
            approve.setApproveTime(DateUtil.date());
            orderCommentApproveService.save(approve);
        }

        orderCommentService.update(comment);
    }

}
